clear
set matsize 11000

********************************************************************************
* Read-in panel bloomberg data
use "./data/fomc_panel_b", clear

label var mps_2day "mps"
label var mpu10_2day "mpu"

******CREATE VARIABLES FOR TABLE 9*****
foreach v in ex_rate_regime us_integ trade_open ka_open fin_depth dollar_exp dollar_debt vuln_index int_diff_3m int_diff_3m_chg {

*Step 1: Standardize each exposure variable, i.e. subtract mean and divide by std. dev.

	sum `v' 
	gen `v'_std = (`v' - r(mean)) / r(sd)
	
*Step 2: Construct a logistic transformation of the standardized variable

	gen `v'_log =  exp(`v'_std) / (1 + exp(`v'_std))
	
*Step 3: Re-center the logistic variable in terms of distance between 25th and 75th percentiles

	sum `v'_log, detail
	gen `v'_exposure = (`v'_log - r(p25)) / (r(p75) - r(p25))
	

*Step 4: Construct interaction term with shocks

	gen `v'_mps_int = `v'_exposure * mps_2day
	
	gen `v'_mpu_int = `v'_exposure * mpu_2day

}	 

*Drop U.S.
drop if ccode==49


********************************************************************************
***Figure 2: 10 Year Yield Response on Prominent Monetary Policy Uncertainty Dates
********************************************************************************
*Identify large mpu changes
egen date_tag = tag(date)
tab date if mpu10_2day>1.2 & date_tag
tab date if mpu10_2day<-2.3 & date_tag

gen 	mpu_flg = 0
replace mpu_flg = 1 if (mpu10_2day>=1.2|mpu10_2day<=-2.3) & mpu10_2day!=.

gen 	mpu_riser = 1 if mpu10_2day>0 & mpu_flg==1
gen 	mpu_faller = 1 if mpu10_2day<0 & mpu_flg==1

bysort mpu_riser: sum mpu10_2day

bysort mpu_faller: sum mpu10_2day

*Save coefficients
foreach k in yield10y {
	
	***Advanced coefficients***
	
	*MPS/MPU
		* Full Sample
			gen `k'_mps_full_adv 		= .
			gen `k'_mpu_full_adv 		= .

	capture noisily quietly: reghdfe `k'_2day_std mps_2day mpu10_2day if advanced==1, noabsorb cluster(ccode time)
	capture noisily replace `k'_mps_full_adv 		= _b[mps_2day]
	capture noisily replace `k'_mpu_full_adv		= _b[mpu10_2day]


	***EME coefficients***

	*MPS/MPU
		* Full Sample
			gen `k'_mps_full_eme 		= .
			gen `k'_mpu_full_eme 		= .
			

	capture noisily quietly: reghdfe `k'_2day_std mps_2day mpu10_2day if advanced==0, noabsorb cluster(ccode time)
	capture noisily replace `k'_mps_full_eme 		= _b[mps_2day]
	capture noisily replace `k'_mpu_full_eme		= _b[mpu10_2day]
	
	***Pooled coefficients***

	*MPS/MPU
		* Full Sample
			gen `k'_mps_full 		= .
			gen `k'_mpu_full 		= .
			

	capture noisily quietly: reghdfe `k'_2day_std mps_2day mpu10_2day, noabsorb cluster(ccode time)
	capture noisily replace `k'_mps_full		= _b[mps_2day]
	capture noisily replace `k'_mpu_full		= _b[mpu10_2day]
	

}

foreach k in yield10y {

	preserve
	
	drop if mpu_riser!=1 & mpu_faller!=1
	replace mpu_riser=0 if missing(mpu_riser)
	replace mpu_faller=0 if missing(mpu_faller)

	collapse (mean) mpu_faller `k'_2day_std mps_2day mpu10_2day `k'_mps_full_adv `k'_mpu_full_adv `k'_mps_full_eme `k'_mpu_full_eme, by(advanced mpu_riser)

	gen 	mps_effect = mps_2day * `k'_mps_full_adv if advanced==1
	replace mps_effect = mps_2day * `k'_mps_full_eme if advanced==0

	gen 	mpu_effect = mpu10_2day * `k'_mpu_full_adv if advanced==1
	replace mpu_effect = mpu10_2day * `k'_mpu_full_eme if advanced==0

	label var `k'_2day_std "Total"
	label var mps_effect "MPS"
	label var mpu_effect "MPU"

	label define adv 0 EME 1 Advanced
	label values advanced adv

	graph bar `k'_2day_std mps_effect mpu_effect if advanced==1 & mpu_riser==1, legend(label(1 "Total") label(2 "MPS") label(3 "MPU")) legend(size(medlarge)) legend(ring(0) position(5) bmargin(large)) ylabel(-1(0.5)1, labsize(medlarge)) yline(0,lcolor(black) lpattern(solid)) bar(1, fcolor(white) lcolor(black)) bar(2, fcolor(black) lcolor(black)) bar(3, fcolor(gs12) lcolor(black))
	graph export "tables\Figure2_MPU_rise_`k'_advanced.png", replace	

	graph bar `k'_2day_std mps_effect mpu_effect if advanced==1 & mpu_faller==1, legend(off) ylabel(-1(0.5)1, labsize(medlarge)) yline(0,lcolor(black) lpattern(solid)) bar(1, fcolor(white) lcolor(black)) bar(2, fcolor(black) lcolor(black)) bar(3, fcolor(gs12) lcolor(black))
	graph export "tables\Figure2_MPU_fall_`k'_advanced.png", replace	

	graph bar `k'_2day_std mps_effect mpu_effect if advanced==0 & mpu_riser==1, legend(off) ylabel(-1(0.5)1, labsize(medlarge)) yline(0,lcolor(black) lpattern(solid)) bar(1, fcolor(white) lcolor(black)) bar(2, fcolor(black) lcolor(black)) bar(3, fcolor(gs12) lcolor(black))
	graph export "tables\Figure2_MPU_rise_`k'_EME.png", replace	

	graph bar `k'_2day_std mps_effect mpu_effect if advanced==0 & mpu_faller==1, legend(off) ylabel(-1(0.5)1, labsize(medlarge)) yline(0,lcolor(black) lpattern(solid)) bar(1, fcolor(white) lcolor(black)) bar(2, fcolor(black) lcolor(black)) bar(3, fcolor(gs12) lcolor(black))
	graph export "tables\Figure2_MPU_fall_`k'_EME.png", replace
	
	restore

}
